//$(".p1").css("display","block");
//计时
function p4_1(x, y, z) {
	var time1 = null;
	time1 = setInterval(function() {
		if(z == 0){
			y--;
			z=100;
		if(y == 0){
			z=0;
			clearInterval(time1);
		}
		}else{
			z--;
			
		}
		$(".hours").html(x + ":" + y + ":" + z);
	}, 1);
}

	function rule1(parameter1,parameter2){
		parameter1.on("touchstart",function(){
			parameter2.css({
				display:"block",
				position:"absolute",
				top:0,
			})
		})
	};
	
	function rule2(parameter1,parameter2){
		parameter1.on("touchstart",function(){
			parameter2.css({
				display:"none",
				position:"absolute",
				top:0,
			})
		})
	}
	
	function rule3(){
		$(".p1_b_top").on("touchstart",function(){
			$(".p3_all").css({
				display:"block",
			})
			$(".p1").css({
				display:"none",
			})
		})
		$(".p2_go").on("touchstart",function(){
			$(".p2_all").css({
				display:"none",
			})
				$(".p3_all").css({
				display:"block",
			})
			$(".p1").css({
				display:"none",
			})
		})
		$(".p3_go").on("touchstart",function(){
			$(".p3_all").css({
				display:"none",
			})
			$(".p4_1").css({
				display:"block",
			})
			var lampArr = ["img/4_1hld_2.png","img/4_1hld_3.png","img/4_1hld_4.png"]
			var x = 0;
			var timer1 = setInterval(function(){
				x++;
				$(".p4_1_deng img").attr("src",lampArr[x]);
				if(x == 3){
					p4_1(0, 60, 100);
					$(".p4_1_deng").css({
						display:"none",
					})
					clearInterval(timer1);
					var speed1 = 0;
					var down = 2;
						var img  = document.createElement("img");
						$(img).attr("src","img/4-1赛道.png");
						$(img).attr("class","img1");
						$(".p4_1_bg .img2").before(img);
							$(".img1").css({
							position:"absolute",
							top:-$(".img2").height()+2,
						})
							
						var img2  = document.createElement("img");
						$(img2).attr("src","img/4-1赛道.png");
						$(img2).attr("class","img3");
						$(".p4_1_bg .img2").before(img2);
							$(".img3").css({
							position:"absolute",
							top:-($(".img2").height()-2)*2,
						})
					setInterval(function(){
						speed1+=down;
						if(speed1>=($(".img2").height()-2)*2){
							speed1=0;
						}
						$(".p4_1").css({
							overflow: "hidden",
						})
						$(".p4_1_bg").css({
							top:speed1+"px",
						})
						$(".p4_1_ready").css({
							display:"none",
						})
							$(".p4_1_star").css({
							display:"none",
						})
					},10);
					var lefts = $(".p4_1_car").offset().left-($(".p4_1_car").width()*2);
					var timer1 = null;
					$(".p4_1_left").on("touchstart",function(){
						timer1 = setInterval(function(){
							$(".p4_1_car").css({
								left:lefts-=2,
							})
								if($(".p4_1_car").offset().left<=30){
								lefts = -$(".p4_1").width()/2-60;
							}
						},1)
						$(".p4_1_left").on("touchend",function(){
							clearInterval(timer1);
						})
						event.preventDefault();
					});
					
					
					$(".p4_1_right").on("touchstart",function(){
						var timer1 = setInterval(function(){
							$(".p4_1_car").css({
								left:lefts+=2,
							})
							if(lefts>=$(".p4_1").width()-$(".p4_1_car").width()-60){
								lefts = $(".p4_1").width()-$(".p4_1_car").width()-60;
							}
						},1)
						$(".p4_1_right").on("touchend",function(){
							clearInterval(timer1);
						})
						event.preventDefault();
					})
					
					$(".p4_1_speed").on("touchstart",function(){
						$(".p4_1_car img").attr("src","img/4-1加速车.png");
						down = 5;
					})
					function rand(min, max) {
						return parseInt(Math.random() * (max - min)) + min;
					}
					var zhangai = 667;
					var i = 0;
					var t = [];
					//asdasdad
					zawd = -10;

						var move_time = null;
						function roadBlock() {
						var height = $(document.body).height();
						var zaTop=-20;
						var	za = $("<img src='img/障碍物.png' style='position: absolute;width: 17%;height:10%;top:-20%;' class='zhangai1'>");
							$(".p4_1").append(za);
							za.css({
								"left":rand(8,75.5)+"%",
							});
						move_time = setInterval(function() {
							zaTop+=down;
							za.css({
								"top":zaTop+"px",
							});
//							console.log(za.offset().top);
							if(za.offset().top>height) {
								console.log(height);
								clearInterval(za.move_time);
								$(za).remove();
//								clearInterval(move_time);
							}
						},10);
					}
							var move2_time = null;
						function roadBlock1() {
						var height1 = $(document.body).height();
						var zaTop1=-20;
						var	za1 = $("<img src='img/障碍车.png' style='position: absolute;width: 20%;height:20%;top:-40%;' class = 'zhangai2'>");
							$(".p4_1").append(za1);
							za1.css({
								"left":rand(8,75.5)+"%",
							});
						move2_time = setInterval(function() {
							zaTop1+=down;
							za1.css({
								"top":zaTop1+"px",
							});
//							console.log(za.offset().top);
							if(za1.offset().top>height1) {
								console.log(height1);
								clearInterval(za1.move2_time);
								$(za1).remove();
//								clearInterval(move2_time);
							}
						},10);
					}
						
						var count = 0;
						var move3_time = null;
					move3_time = setInterval(function(){
						count++;
						if(count%2 == 0){
							roadBlock();
						}else if(count%5 == 0){
							roadBlock1();
						}else if(count%2 == 0&&count%5 == 0){
							roadBlock();
						}
			var snake = document.getElementsByClassName("p4_1_car")[0];
			var food = document.getElementsByClassName("zhangai1");
			var food2 = document.getElementsByClassName("zhangai2");
			
			for(var i = 0;i<food.length;i++){
			if(snake.offsetLeft+snake.offsetWidth>=food[i].offsetLeft&&snake.offsetTop+snake.offsetHeight>=food[i].offsetTop&&food[i].offsetLeft+food[i].offsetWidth>=snake.offsetLeft&&food[i].offsetTop+food[i].offsetHeight>=snake.offsetTop){
//					alert("over");
					$(".p4_1").css({
						display:"none",
					})
						$(".p5b_all").css({
						display:"block",
					})
						clearInterval(move3_time);
						return;
			} 
	
		}
			for(var j = 0;j<food2.length;j++){
				if(snake.offsetLeft+snake.offsetWidth>=food2[j].offsetLeft&&snake.offsetTop+snake.offsetHeight>=food2[j].offsetTop&&food2[j].offsetLeft+food2[j].offsetWidth>=snake.offsetLeft&&food2[j].offsetTop+food2[j].offsetHeight>=snake.offsetTop){
//					alert("over");
	$(".p4_1").css({
						display:"none",
					})
						$(".p5b_all").css({
						display:"block",
					})
						clearInterval(move3_time);
						return;
						console.log(123);
			} 		
			}
					},1000)
//					console.log($(".zhangai1"));
				}
			},1000)
		})
	}
p4_1(0,0,0);
